/**
 * Created by yangsl on 2016/6/23.
 */
jQuery(document).ready(function() {
    var $modal = $('#ajax-modal');
    $('.modal-btn').on('click', function() {
        var el = $(this);
        $modal.load(el.attr('data-url'), '', function(){
            $modal.modal();
        });
    });

    $modal.on('click', 'button[type="submit"]', function(e){
        var name = $.trim($("input[name='name']").val());
        if(name == "") {
            toastr['error']('名称不能为空', '');
            return false;
        }
        var descNode = $("input[name='description']");
        if(descNode.length == 0) {
            descNode = $("textarea[name='description']");
        }

        if(descNode.length > 0) {
            var description = $.trim(descNode.val());
            if(description == "") {
                toastr['error']('描述不能为空', '');
                return false;
            }
        }

        var passwordNode = $("input[name='password']");
        if(passwordNode.length > 0) {
            var password = $.trim(passwordNode.val());
            if(password.length < 6) {
                toastr['error']('密码不能少于6位', '');
                return false;
            }
        }

        var emailNode = $("input[name='email']");
        if(emailNode.length > 0) {
            var email = $.trim(emailNode.val());
            var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
            if(!reg.test(email)) {
                toastr['error']('邮件格式不正确', '');
                return false;
            }
        }

    });

});

